package com.sisgh.jefdv1.core.java.keyword.pack;

import com.sisgh.jefdv1.exception.java.JavaFormatException;
import com.sisgh.jefdv1.util.StringUtils;
import lombok.Data;

import java.util.regex.Pattern;

@Data
public class Package {

    private static final Pattern pattern = Pattern.compile("^[a-z_][a-z0-9_$]*(\\.[a-z_][a-z0-9_$]*)*$");

    private String path;

    private Package(String path) {
        this.path = path;
    }

    public static Package of(String path) throws JavaFormatException {
        if (!pattern.matcher(path).matches()) {
            throw new JavaFormatException("Invalid package path: " + path);
        }
        return new Package(path);
    }

    public boolean isEmpty() {
        return StringUtils.isNullOrEmpty(path);
    }

    public String toFilePath() {
        return path.replace('.', '/');
    }

    @Override
    public String toString() {
        return "package " + path;
    }
}
